package com.atguigu.gmall.common.interceptor;

import com.atguigu.gmall.common.constant.RedisConst;
import com.atguigu.gmall.common.util.UserAuthUtil;
import feign.RequestInterceptor;
import feign.RequestTemplate;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpServletRequest;

//解决feign请求头丢失问题
@Component
public class UserAuthFeignInterceptor implements RequestInterceptor {
    @Override
    public void apply(RequestTemplate requestTemplate) {
        //1获取老请求
//        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
//        attributes.getRequest();
        HttpServletRequest request = UserAuthUtil.getRequest();
        //传 userId
        String userId = request.getHeader(RedisConst.USER_ID_HEADER);
        if (!StringUtils.isEmpty(userId)){
            requestTemplate.header(RedisConst.USER_ID_HEADER, userId);
        }
        //传 tempId
        String tempId = request.getHeader(RedisConst.TEMP_ID_HEADER);
        if (!StringUtils.isEmpty(tempId)){
            requestTemplate.header(RedisConst.TEMP_ID_HEADER, tempId);
        }
    }
}
